package com.jrx.anytxn.transaction.job.cmpsCrt.config;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.handler.IPaymentHandler;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;


/**
 * 代偿回购分配
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class CmpsCrtAllocationConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(CmpsCrtAllocationConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public IPaymentHandler paymentHandler;

    @Resource
    private ExtTlPaymentLogMapper extTlPaymentLogMapper;


    @Bean
    public Job cmpsCrtAllocation() {
        return jobBuilderFactory.get("cmpsCrtAllocation")
                .start(cmpsCrtAllocationStep())
                .build();
    }

    @Bean
    public Step cmpsCrtAllocationStep() {
        return stepBuilderFactory.get("cmpsCrtAllocationStep")
                .tasklet(cmpsCrtAllocationTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet cmpsCrtAllocationTasklet() {
        return (stepContribution, chunkContext) -> {
            List<TlPaymentLog> logList = extTlPaymentLogMapper.selectBySumStatusAndPaymentTypeList(
                    TransBizConstant.PAYMENT_LOG_SUM_STATUS_T, Arrays.asList(TransBizConstant.REPAYMENT_TYPE_CMPS,TransBizConstant.REPAYMENT_TYPE_CRT));
            List<CompletableFuture<Void>> list = logList.stream().map(log ->
                    CompletableFuture.runAsync(() ->
                            {
                                try {
                                    paymentHandler.cmpsCrtAllocation(log);
                                } catch (TxnBizException e) {
                                    logger.error("代偿回购分配差错异常：",e);
                                }
                            }
                    )
            ).collect(Collectors.toList());

            long count = list.stream().map(CompletableFuture::join).count();
            logger.info("代偿回购还款分期最终结果数{}",count);
            return RepeatStatus.FINISHED;
        };
    }
}
